﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Database.Service;
using Portal.Web.Utility;

namespace Portal.Cms.Bussiness
{
    public class ReadingServiceBll
    {
        public static CmsDataCls[] ReadingListNewsForAlias(string SiteId, string Alias, int ActiveSortIndex)
        {

            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query = "    select TableData.* from TableData " +
                           "    join TableGroupData on TableGroupData.GroupDataId = frkGroupDataId" +
                           "    where IsPublic = @IsPublic and Alias=@Alias and ParentDataId is null ";

            if (ActiveSortIndex == 1)
            {
                Query +=
                           "        order by TableData.SortIndex ";
            }
            else
            {
                Query +=
                           "        order by EntryDate DESC";
            }
            DataSet dsResult =
                    DBService.GetDataSet(null, Query, new DbParam[]{
                        new DbParam("Alias",Alias),
                        new DbParam("IsPublic",1),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new CmsDataCls[0];
            }

            CmsDataCls[] CmsDatas = CmsDataParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsDatas;
        }


        public static CmsDataCls[] ReadingTopListNewsForAlias(string SiteId, string Alias,int Top,int ActiveSortIndex)
        {
            if (Top == 0)
            {
                return ReadingListNewsForAlias(SiteId, Alias,ActiveSortIndex);
            }

            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);

            string Query = "    select TOP " + Top + "  TableData.* from TableData " +
                           "    left outer join TableGroupData on TableGroupData.GroupDataId = frkGroupDataId" +
                           "    where IsPublic = @IsPublic and Alias=@Alias and ParentDataId is null ";
            if (ActiveSortIndex==1)
            {
                Query +=
                           "        order by TableData.SortIndex ";
            }
            else
            {
                Query +=
                           "        order by EntryDate DESC";
            }

            DataSet dsResult =
                    DBService.GetDataSet(null, Query, new DbParam[]{
                        new DbParam("Alias",Alias),
                        new DbParam("IsPublic",1),
                    });

            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new CmsDataCls[0];
            }

            CmsDataCls[] CmsDatas = CmsDataParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsDatas;
        }

        public static GroupDataCls[] ReadingAlbum(string SiteId, int ActiveSortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            //string Query = " select TableData.* from TableData " +
            //               "        left outer join TableGroupData on TableGroupData.GroupDataId = frkGroupDataId" +
            //               "    where IsPublic = 1 and ParentDataId is null and frkDataTypeId='album'";
            //if (ActiveSortIndex == 1)
            //{
            //    Query +=
            //               "        order by TableData.SortIndex ";
            //}
            //else
            //{
            //    Query +=
            //               "        order by EntryDate DESC";
            //}

            string OrderBy = "";
            if (ActiveSortIndex == 1)
            {
                OrderBy =
                           "        order by SortIndex ";
            }
            else
            {
                OrderBy =
                           "        order by EntryDate DESC";
            }
            string Query = " select TableGroupData.*,Avatar=(select top 1 avatar from TableData where frkGroupDataId=GroupDataId and IsPublic = 1 and ParentDataId is null " + OrderBy + ") from TableGroupData " +
                           "    where  frkDataTypeId='album' " + OrderBy;
            
            DataSet dsResult =
                    DBService.GetDataSet(null, Query, null);
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new GroupDataCls[0];
            }
            GroupDataCls[] GroupDatas = GroupDataParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return GroupDatas;
        }


        public static CmsDataCls[] ReadingListNewsExclusive(string SiteId, string GroupDataId, string ExclusiveDataId, int ActiveSortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query = " select TableData.* from TableData " +
                           "        left outer join TableGroupData on TableGroupData.GroupDataId = frkGroupDataId" +
                           "    where IsPublic = 1 and frkGroupDataId=@GroupDataId and DataId<>@ExclusiveDataId and ParentDataId is null ";
            if (ActiveSortIndex == 1)
            {
                Query +=
                           "        order by TableData.SortIndex ";
            }
            else
            {
                Query +=
                           "        order by EntryDate DESC";
            }
            DataSet dsResult =
                    DBService.GetDataSet(null, Query, new DbParam[]{
                        new DbParam("GroupDataId",GroupDataId),
                        new DbParam("ExclusiveDataId",ExclusiveDataId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new CmsDataCls[0];
            }
            CmsDataCls[] CmsDatas = CmsDataParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsDatas;
        }


        public static CmsDataCls[] ReadingTopSubListNewsForAlias(string SiteId, string ParentDataId, int ActiveSortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query = " select TableData.* from TableData " +
                           "        left outer join TableGroupData on TableGroupData.GroupDataId = frkGroupDataId" +
                           "    where IsPublic = 1 and ParentDataId=@ParentDataId ";
            if (ActiveSortIndex == 1)
            {
                Query +=
                           "        order by TableData.SortIndex ";
            }
            else
            {
                Query +=
                           "        order by EntryDate DESC";
            }
            DataSet dsResult =
                    DBService.GetDataSet(null, Query, new DbParam[]{
                        new DbParam("ParentDataId",ParentDataId)
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new CmsDataCls[0];
            }
            CmsDataCls[] CmsDatas = CmsDataParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsDatas;
        }
    }
}
